﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Uinside.CreditSystem.DomainModels.Models;

namespace Uinside.CreditSystem.Services
{
    public class OrderService : BaseService
    {
        public OrderService(CreditContext dbContext)
            : base(dbContext)
        {
        }

        public IEnumerable<Order> GetAll()
        {
            return _creditContext.Orders; //.Where(i => i.Id != 1);
        }

        public string GetOrderNumber()
        {
            var stringOfNow = DateTime.Now.ToString("yyyyMMdd");

            var now = DateTime.Now;
            var orderCreatedToday = _creditContext.Orders.Where(i => i.CreateDate.Year == now.Year && i.CreateDate.Month == now.Month&&
                i.CreateDate.Day == now.Day);

            var maxId = 1;
            if (orderCreatedToday.Any())
            {
                maxId = orderCreatedToday.Max(i => i.Id) + 1;
            }

            return stringOfNow + string.Format("{0:00000}", maxId);
        }
    }
}
